package com.shxc.smarttrade.old.utils;

import android.util.Log;

/**
 * Created by jcdev1 on 2017/6/1.
 */

public class CountClass {
    /**
     * @param closePrice    //设置的值
     * @param openPrice     //开仓价
     * @param cmd
     * @param ask
     * @param bid
     * @param contracSize
     * @param volume
     * @param profitChange
     * @param profitUSDPrex
     * @return
     */
    public static double getProfit(double closePrice, double openPrice, int cmd, double ask, double bid,
                                   int contracSize, double volume, boolean profitChange, boolean profitUSDPrex) {
        double priceTrans;
        double priceCal;
        double profit;
        if (cmd == 0) {//买涨
            priceCal = Arith.mul(Arith.mul(Arith.sub(closePrice, openPrice), contracSize), volume);
        } else {//买跌
            priceCal = Arith.mul(Arith.mul(Arith.sub(openPrice, closePrice), contracSize), volume);
        }
        if (profitChange) {
            if (cmd == 0) {
                priceTrans = bid;
            } else {
                priceTrans = ask;
            }
            if (priceTrans == 0) return 0;
            if (profitUSDPrex) {
                profit = Arith.div(priceCal, priceTrans, 2);
            } else {
                profit = Arith.round(Arith.mul(priceCal, priceTrans), 2);
            }
        } else {
            profit = Arith.round(priceCal, 2);
        }
        return profit;
    }

    /**
     * @param askBidOne     [0]:ask [1]:bid  当前项实时价
     * @param openPrice
     * @param cmd
     * @param askBidThree   [0]:ask  [1]:bid 收益货币转换对的实时价
     * @param contracSize
     * @param volume
     * @param profitChange
     * @param profitUSDPrex
     * @return
     */
    public static double getProfit(double[] askBidOne, double openPrice, int cmd, double[] askBidThree,
                                   int contracSize, double volume, boolean profitChange, boolean profitUSDPrex) {
        double priceTrans;
        double priceCal;
        double profit;
        if (cmd == 0) {//买涨
            priceCal = Arith.mul(Arith.mul(Arith.sub(askBidOne[1], openPrice), contracSize), volume);
        } else {//买跌
            priceCal = Arith.mul(Arith.mul(Arith.sub(openPrice, askBidOne[0]), contracSize), volume);
        }
        if (profitChange) {
            if (cmd == 0) {
                priceTrans = askBidThree[1];
            } else {
                priceTrans = askBidThree[0];
            }

            if (profitUSDPrex) {
                profit = Arith.div(priceCal, priceTrans, 2);
            } else {
                profit = Arith.round(Arith.mul(priceCal, priceTrans), 2);
            }
        } else {
            profit = Arith.round(priceCal, 2);
        }
        return profit;
    }

    /**
     * 实时盈亏计算
     *
     * @param setPrice
     * @param cmd
     * @param askBidThree   [0]:ask  [1]:bid
     * @param contracSize
     * @param volume
     * @param profitChange
     * @param profitUSDPrex
     * @return
     */
    public static double getProfit(double setPrice, int cmd, double[] askBidOne, double[] askBidThree,
                                   int contracSize, double volume, boolean profitChange, boolean profitUSDPrex) {
        double priceTrans;
        double priceCal;
        double profit;
        Log.e("setPrice", setPrice + "  askBidOne[0]:" + askBidOne[0] + " contracSize:" + contracSize + " volume:" + volume);
        if (cmd == 0) {
            if (askBidOne == null) return -1;
            priceCal = Arith.mul(Arith.mul(Arith.sub(setPrice, askBidOne[0]), contracSize), volume);

        } else {
            if (askBidOne == null) return -1;
            priceCal = Arith.mul(Arith.mul(Arith.sub(askBidOne[1], setPrice), contracSize), volume);

        }
        if (profitChange) {

            if (cmd == 0) {
                if (askBidThree == null) return -1;
                priceTrans = askBidThree[1];
            } else {
                if (askBidThree == null) return -1;
                priceTrans = askBidThree[0];
            }
            if (profitUSDPrex) {
                profit = Arith.div(priceCal, priceTrans, 2);

            } else {
                profit = Arith.mul(priceCal, priceTrans);

            }
        } else {
            profit = priceCal;
        }
        return profit;
    }

    /**
     * 计算保证金
     *
     * @param type
     * @param lots
     * @param askBidOne    [0]:ask  [1]:bid
     * @param askBidTwo    [0]:ask  [1]:bid
     * @param cmd
     * @param marginBase
     * @param marginChange
     * @param usdprex
     * @return
     */
    public static double getMargin(int type, double lots, double[] askBidOne, double[] askBidTwo, int cmd, double marginBase, boolean marginChange, boolean usdprex) {
        double marginCalc;
        double margin;
        if (type == 1) {
            if (askBidOne == null) return -1;
            if (cmd == 0) {//买涨
                marginCalc = Arith.round(Arith.mul(Arith.mul(lots, askBidOne[0]), marginBase), 2);

            } else {//买跌
                marginCalc = Arith.round(Arith.mul(Arith.mul(lots, askBidOne[1]), marginBase), 2);
            }
        } else {
            marginCalc = Arith.round(Arith.mul(lots, marginBase), 2);
        }

        if (marginChange) {
            if (askBidTwo == null) return -1;
            double price = Arith.div(Arith.add(askBidTwo[0], askBidTwo[1]), 2);
            if (usdprex) {
                margin = Arith.div(marginCalc, price, 2);
            } else {
                margin = Arith.round(Arith.mul(marginCalc, price), 2);
            }
        } else {
            margin = marginCalc;
        }
        return margin;
    }
}
